﻿// Decompiled with JetBrains decompiler
// Type: XamlEditor.PathCreator
// Assembly: XamlEditor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 2AF0471B-7BFE-4738-893B-A4F91E243353
// Assembly location: C:\Users\KasimGulcan\Desktop\Kosgeb\DynaCAD\Project\lib\XamlEditor.dll

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace XamlEditor
{
  public class PathCreator
  {
    public static Path LineCreater(Point a, Point b)
    {
      Path path = new Path();
      LineGeometry lineGeometry = new LineGeometry(a, b);
      path.Data = (Geometry) lineGeometry;
      return path;
    }

    public static Path PolyLineCreater(List<Point> a)
    {
      int num = 0;
      Path path = new Path();
      PathGeometry pathGeometry = new PathGeometry();
      PathFigure pathFigure = new PathFigure();
      PolyLineSegment polyLineSegment = new PolyLineSegment();
      foreach (Point point1 in a)
      {
        if (num != 0)
        {
          polyLineSegment = new PolyLineSegment();
          foreach (Point point2 in a)
            polyLineSegment.Points.Add(point2);
        }
        ++num;
      }
      pathFigure.Segments.Add((PathSegment) polyLineSegment);
      pathGeometry.Figures.Add(pathFigure);
      pathFigure.StartPoint = a[0];
      path.Data = (Geometry) pathGeometry;
      return path;
    }

    public static Path ArcCreater(List<Point> c)
    {
      Path path = new Path();
      double num1 = (c[0].X - c[1].X) / (c[1].Y - c[0].Y);
      double num2 = (c[2].X - c[1].X) / (c[1].Y - c[2].Y);
      double num3 = (c[0].Y + c[1].Y) / 2.0 - (Math.Pow(c[0].X, 2.0) - Math.Pow(c[1].X, 2.0)) / (2.0 * c[1].Y - 2.0 * c[0].Y);
      double num4 = (c[1].Y + c[2].Y) / 2.0 - (Math.Pow(c[1].X, 2.0) - Math.Pow(c[2].X, 2.0)) / (2.0 * c[2].Y - 2.0 * c[1].Y);
      double num5 = (num4 - num3) / (num1 - num2);
      double num6 = num2 * num5 + num4;
      double num7 = Math.Sqrt(Math.Pow(c[1].X - num5, 2.0) + Math.Pow(c[1].Y - num6, 2.0));
      SweepDirection sweepDirection;
      double num8;
      if ((c[0].Y - c[1].Y) / (c[1].X - c[0].X) > (c[0].Y - c[2].Y) / (c[2].X - c[0].X))
      {
        sweepDirection = SweepDirection.Clockwise;
        num8 = Vector.AngleBetween(new Vector(c[0].X - num5, c[0].Y - num6), new Vector(c[2].X - num5, c[2].Y - num6));
      }
      else
      {
        sweepDirection = SweepDirection.Counterclockwise;
        num8 = -Vector.AngleBetween(new Vector(c[2].X - num5, num6 - c[2].Y), new Vector(c[0].X - num5, num6 - c[0].Y));
      }
      ArcSegment arcSegment = new ArcSegment(new Point(c[2].X, c[2].Y), new Size(num7, num7), 0.0, num8 < 0.0, sweepDirection, true);
      PathGeometry pathGeometry = new PathGeometry();
      PathFigure pathFigure = new PathFigure();
      pathFigure.Segments.Add((PathSegment) arcSegment);
      pathGeometry.Figures.Add(pathFigure);
      pathFigure.StartPoint = c[0];
      path.Data = (Geometry) pathGeometry;
      return path;
    }

    public static Path BezierCreater(List<Point> b)
    {
      Path path = new Path();
      PathGeometry pathGeometry = new PathGeometry();
      PathFigure pathFigure = new PathFigure();
      BezierSegment bezierSegment = new BezierSegment();
      pathGeometry.Figures.Add(pathFigure);
      pathFigure.Segments.Add((PathSegment) bezierSegment);
      path.Data = (Geometry) pathGeometry;
      pathFigure.StartPoint = b[0];
      bezierSegment.Point1 = b[1];
      bezierSegment.Point2 = b[2];
      bezierSegment.Point3 = b[3];
      return path;
    }

    public static Path QuadraticBezierCreater(List<Point> p)
    {
      Path path = new Path();
      PathGeometry pathGeometry = new PathGeometry();
      PathFigure pathFigure = new PathFigure();
      QuadraticBezierSegment quadraticBezierSegment = new QuadraticBezierSegment();
      pathGeometry.Figures.Add(pathFigure);
      pathFigure.Segments.Add((PathSegment) quadraticBezierSegment);
      path.Data = (Geometry) pathGeometry;
      pathFigure.StartPoint = p[0];
      quadraticBezierSegment.Point1 = p[1];
      quadraticBezierSegment.Point2 = p[2];
      return path;
    }

    public static Path SplineCreater(List<Point> s)
    {
      PathFigure pathFigure = new PathFigure();
      PathGeometry pathGeometry = new PathGeometry();
      PathSegmentCollection segmentCollection = new PathSegmentCollection();
      Path path = new Path();
      if (s.Count == 2)
        return PathCreator.LineCreater(s[0], s[1]);
      if (s.Count == 3)
        return PathCreator.QuadraticBezierCreater(s);
      if (s.Count == 4)
        return PathCreator.BezierCreater(s);
      if (s.Count > 4)
      {
        int num1 = Convert.ToInt32((s.Count - 4) / 2) + 1;
        int num2 = s.Count % 2;
        for (int index1 = 0; index1 < num1; ++index1)
        {
          if (index1 == 0)
          {
            List<Point> b = new List<Point>();
            for (int index2 = 0; index2 < 4; ++index2)
              b.Add(s[index2]);
            segmentCollection.Add(((PathGeometry) PathCreator.BezierCreater(b).Data).Figures[0].Segments[0]);
          }
          else
          {
            int index2 = index1 * 2 + 2 - 1;
            segmentCollection.Add(((PathGeometry) PathCreator.BezierCreater(new List<Point>()
            {
              new Point(0.0, 0.0),
              new Point(s[index2].X + s[index2].X - s[index2 - 1].X, s[index2].Y + s[index2].Y - s[index2 - 1].Y),
              s[index2 + 1],
              s[index2 + 2]
            }).Data).Figures[0].Segments[0]);
          }
        }
        if (num2 == 1)
        {
          int index = s.Count - 2;
          segmentCollection.Add(((PathGeometry) PathCreator.QuadraticBezierCreater(new List<Point>()
          {
            new Point(0.0, 0.0),
            new Point(s[index].X + s[index].X - s[index - 1].X, s[index].Y + s[index].Y - s[index - 1].Y),
            s[s.Count - 1]
          }).Data).Figures[0].Segments[0]);
        }
      }
      pathFigure.StartPoint = s[0];
      pathFigure.Segments = segmentCollection;
      pathGeometry.Figures.Add(pathFigure);
      path.Data = (Geometry) pathGeometry;
      return path;
    }

    public static Path RectangleCreater(List<Point> r)
    {
      return new Path()
      {
        Data = (Geometry) new RectangleGeometry()
        {
          Rect = new Rect(r[0], r[1])
        }
      };
    }

    public static Path EllipseCreater(List<Point> e)
    {
      Path path = new Path();
      EllipseGeometry ellipseGeometry = new EllipseGeometry();
      ellipseGeometry.RadiusX = (e[1].X - e[0].X) / 2.0;
      ellipseGeometry.RadiusY = (e[1].Y - e[0].Y) / 2.0;
      double x = e[0].X + ellipseGeometry.RadiusX;
      double y = e[0].Y + ellipseGeometry.RadiusY;
      ellipseGeometry.Center = new Point(x, y);
      path.Data = (Geometry) ellipseGeometry;
      return path;
    }
  }
}
